<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: events.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: events.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>// 使用w3cwebsocket对象，兼容Browser和Node环境
import { w3cwebsocket as WebSocket } from "websocket";

class EventTube {
    /**
     * @callback websocketCallback
     * @param {Object} eventMessage
    */
    /**
     * 构建事件订阅对象实例
     * 
     * @param {string} address websocket服务地址
     * @param {websocketCallback} cb 处理返回信息的回调函数
     * @param {number} timeout 重连的时间间隔
     */
    constructor(address, cb, timeout) {
        this._address = address;
        this._cb = cb;
        this._timeout = timeout || 5000;
        this.timer = null;
        this.ws = null;
        this.connect();
    }

    reconnect() {
        const me = this;
        const timeout = me._timeout;
        if (!me.timer) {
            me.timer = setTimeout(() => {
                me.connect();
            }, timeout);
        }
    }

    connect() {
        const me = this;
        me.timer = null;
        console.log(`connecting ${me._address}`);
        const ws = new WebSocket(me._address);
        me.ws = ws;
        ws.onerror = (evt) => {
            console.log(`error:${evt.message}`);
            me.reconnect();
        };
        ws.onmessage = (m) => {
            me._cb(m);
        };
        ws.onopen = () => {
            console.log("connected");
        };
        ws.onclose = (e) => {
            console.log("disconnected");
            if (e.code !== 4000) { 
                me.reconnect(); 
            } else {
                console.log(`for the reason: ${e.reason}`);
            }
        };
    }

    /**
     * 主动关闭websocket连接
     * 
     * @param {String} reason 解释主动关闭连接的原因，不超过123字节
     */
    close(reason) {
        this.ws.close(4000, reason);
    }
}
export default EventTube;
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="EventTube.html">EventTube</a></li><li><a href="RestAPI.html">RestAPI</a></li><li><a href="Transaction.html">Transaction</a></li></ul><h3>Global</h3><ul><li><a href="global.html#CalculateAddr">CalculateAddr</a></li><li><a href="global.html#CreateCertificate">CreateCertificate</a></li><li><a href="global.html#CreateCSR">CreateCSR</a></li><li><a href="global.html#CreateKeypair">CreateKeypair</a></li><li><a href="global.html#CreateSelfSignedCertificate">CreateSelfSignedCertificate</a></li><li><a href="global.html#GetCSRInfoFromPEM">GetCSRInfoFromPEM</a></li><li><a href="global.html#GetHashVal">GetHashVal</a></li><li><a href="global.html#GetKeyPEM">GetKeyPEM</a></li><li><a href="global.html#ImportCertificate">ImportCertificate</a></li><li><a href="global.html#ImportKey">ImportKey</a></li><li><a href="global.html#Sign">Sign</a></li><li><a href="global.html#VerifyCertificateSignature">VerifyCertificateSignature</a></li><li><a href="global.html#VerifySign">VerifySign</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun Sep 27 2020 14:29:27 GMT+0800 (China Standard Time)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
